/* *********
 *    Author: Jason He;
 *    Version: 0.0.1;
 *    Date: 2016-12-16;
 *    File: common.js;
 ********* */

$(document).ready(function () {
	// links
	$(".footer-links-btn").click(function () {
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
			$(this).prev().css("height", "28px");
		} else {
			$(this).addClass("active");
			$(this).prev().css("height", "auto");
		}
	});

	// phone validate
	$("#sidebar-phone-input").change(function () {
		$("#sidebar-phone-error").hide();
	});
	$("#sidebar-phone-btn").click(function () {
		var phone = $("#sidebar-phone-input").val();
		//alert(phone);
		if (phone) {
			var reg = new RegExp(/^(1[34578]\d{9})|^(0\d{2,3}-?\d{7,8})$/);
			if (reg.test(phone)) {
				//alert("瓜子客服正在拨通您的电话，免费的哦，请您放心接听。");
				$.ajax({
				  type: 'POST',
				  url: "/Home/Seller/addPhone",
				  data: {phone:phone,carid:$('#carid').val()},
				  dataType:'json',
				  success:function(res){
					  if(res.id==1)
					  {
						  alert(res.msg);
					  }else
					  {
						  alert('出现异常，请联系客服');
					  }
				  }
				});
			} else {
				$("#sidebar-phone-error").text("请填写正确的手机号码哦~").show();
				$("#sidebar-phone-input").focus();
				return false;
			}
		} else {
			$("#sidebar-phone-error").text("忘记填写手机号码了哦~").show();
			$("#sidebar-phone-input").focus();
			return false;
		}
	});

	// sidebar button
	$("#sidebar-btn-close").click(function () {
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
			$(".sidebar-close").show();
			$("#sidebar-qrcode-app").hide();
			$("#sidebar-qrcode-wx").hide();
		} else {
			$(this).addClass("active");
			$(".sidebar-close").hide();
		}
	});
	$("#sidebar-btn-phone").click(function () {
		$(".sidebar-switch").hide();
		$("#sidebar-phone").show();
	});
	$("#sidebar-btn-feedback").click(function () {
		$("#feedback").show();
	});
	$("#sidebar-btn-app").click(function () {
		$(".sidebar-switch").hide();
		$("#sidebar-qrcode-app").show();
	});
	$("#sidebar-btn-wx").click(function () {
		$(".sidebar-switch").hide();
		$("#sidebar-qrcode-wx").show();
	});
	$("#sidebar-btn-top").click(function () {
		$(document).scrollTop(0);
	});

	// feedback
	$("#feedback-close").click(function () {
		$("#feedback").hide();
		$("#feedback-content").val("");
		$("#feedback-contact").val("");
		$("#feedback-error").text("");
	});
	$("#feedback-submit").click(function () {
		if (!$("#feedback-content").val()) {
			$("#feedback-error").text("您还没有填写意见呢~");
			return false;
		} else {
			alert("感谢您提出的宝贵意见！");
			$("#feedback").hide();
			$("#feedback-content").val("");
			$("#feedback-contact").val("");
			$("#feedback-error").text("");
		}
	});
});
//搜索
function search()
{
    var key = $('#keywords').val();
    jumpUrl("ke", key)
}
//地址跳转
function jumpUrl(key,value) {
	url = "/home/index/get_car_list?pa=0&ma=40";
    window.location.href = makeurl(key, value, url);
}
//地址处理
function makeurl(key, value, url) {
    if (key == 'or')
        url += '&' + key + '=' + value;
    else
        url += "&or={$or}";
    if (key == 'ci')
        url += '&' + key + '=' + value;
    else
        url += "&ci={$Think.session.citype}";
    if (key == 'pl')
        url += '&' + key + '=' + value;
    else
        url += "&pl={$Think.session.mycity}";
    if (key == 'ke')
        url += '&' + key + '=' + value;
    else
        url += "&ke={$getData.ke}";
    var resetse = false;
    console.log("{$getData.br}");
    if (key == 'br')
    {
        url += '&br=' + value;
        resetse = true;
    }
    else
    {
        if ($("#brand .cur").attr('id')) {
            url += "&br={$getData.br}&bid=" + $("#brand .cur").attr('id');
        }
        else {
            url += "&br={$getData.br}&bid=";
        }
       
    }
    if (key == 'se')
        url += '&' + key + '=' + value;
    else
    {
        if (resetse)
            url += "&se=";
        else
            url += "&se={$getData.se}";
    }
        
    if (key == 'pr')
        url += '&' + key + '=' + value;
    else
        url += "&pr={$getData.pr}";
    if (key == 'mo')
        url += '&' + key + '=' + value;
    else
        url += "&mo={$getData.mo}";
    if (key == 'ta')
        url += '&' + key + '=' + value;
    else
        url += "&ta={$getData.ta}";
    if (key == 'ag')
        url += '&' + key + '=' + value;
    else
        url += "&ag={$getData.ag}";
    if (key == 'lo')
        url += '&' + key + '=' + value;
    else
        url += "&lo={$getData.lo}";
    if (key == 'di')
        url += '&' + key + '=' + value;
    else
        url += "&di={$getData.di}";
    if (key == 'en')
        url += '&' + key + '=' + value;
    else
        url += "&en={$getData.en}";
    if (key == 'oi')
        url += '&' + key + '=' + value;
    else
        url += "&oi={$getData.oi}";
    if (key == 'con')
        url += '&' + key + '=' + value;
    else
        url += "&con={$getData.con}";
    if (key == 'sea')
        url += '&' + key + '=' + value;
    else
        url += "&sea={$getData.sea}";
    if (key == 'wat')
        url += '&' + key + '=' + value;
    else
        url += "&wat={$getData.wat}";
    url += "&total={$total}";
    return url;
}

//登录 显示
function login(){
	$("#login").show();
	$(".close").click(function () {
		$(this).parent().parent().parent().hide();
	});
}
//注册 显示
function register(){
	$("#register").show();
	$(".close").click(function () {
		$(this).parent().parent().parent().hide();
	});
}
//点击登录
function click_login(){
	var username = $('#username').val();
	var password = $('#password').val();
	var img_code = $('#img_code').val();
	var flag = true;
	if(username.length == 0){
		$('#no_username').show();
		flag = false;
	}else{
		$('#no_username').hide();
	}
	if(password.length == 0){
		$('#no_password').show();
		flag = false;
	}else{
		$('#no_ password').hide();
	}
	//验证码验证
	$.post("/Home/Post/check_verify",{img_code:img_code},function(res){
		if(res != 1){
			flag = false;
			$('#no_img_code').show();
		}else{
			$('#no_img_code').hide();
		}
	})
	//提交
	if(flag == true){
		$.post('/Home/Logo/login',{username:username,password:password},function(res){
			if(res == 1){
				//登录成功
				window.location.reload();
			}
		})
	}
}
//点击注册
function click_reg(){
	var username = $('#reg_username').val();
	var password = $('#reg_password').val();
	var img_code = $('#reg_img_code').val();
	var phone_code = $('#reg_phone_code').val();
	var flag = true;
	if(username.length == 0){
		$('#no_reg_username').text('忘记填写手机号码了哦')
		$('#no_reg_username').show();
		flag = false;
	}else{
		$('#no_reg_username').hide();
	}
	if(password.length == 0){
		$('#no_reg_password').show();
		flag = false;
	}else{
		$('#no_reg_password').hide();
	}
	if(phone_code.length == 0){
		$('#no_reg_phone_code').show();
	}else{
		$('#no_reg_phone_code').hide();
	}
	//验证码验证
	$.post("/Home/Post/check_verify",{img_code:img_code},function(res){
		if(res != 1){
			flag = false;
			$('#reg_img_code').show();
		}else{
			$('#reg_img_code').hide();
		}
	})
	//提交
	if(flag == true){
		$.post('/Home/Logo/register',{username:username,password:password,code:phone_code},function(res){
			if(res.err == 0){
				//登录成功
				window.location.reload();
			}else{
				alert(res.msg);
			}
		},'json')
	}
}
//获取手机验证码
function phone_code(){
	var phone = $('#reg_username').val();
	if (phone) {
		var reg = new RegExp(/^1[34578]\d{9}$/);
		if (reg.test(phone)) {
			$.ajax({
			  type: 'POST',
			  url: "/Home/Post/getCode",
			  data:{phone:phone,reg:1},
			  dataType:'json',
			  success:function(res){
				  $('#reg_yzm').val("60秒");
			  }
			});
			
			var num = 60;
			var timer = setInterval(function () {
				num--;
				$("#reg_yzm").val(num + "秒");
				if (num == 0) {
					clearInterval(timer);
					$("#reg_yzm").val("获取验证码");
				}
			}, 1000);
		} else {
			$('#no_reg_username').text("请填写正确的手机号码哦~");
			$('#no_reg_username').show();
			return false;
		}
	}else{
		$('#no_reg_username').text('忘记填写手机号码了哦')
		$('#no_reg_username').show();
	}
}